<?php

namespace App\Http\Controllers\V1;

use App\Events\V1\UserPushEvent;
use App\Events\V1\UserCountChannelEvent;
use App\Helpers\DateUtils;
use App\Helpers\Generator\TokenGenerator;
use App\Helpers\Logger\SLogger;
use App\Helpers\RestResponseFactory;
use App\Helpers\RestUtils;
use App\Http\Controllers\Controller;
use App\Models\Factory\SmsFactory;
use App\Models\Factory\UserFactory;
use App\Models\Orm\User;
use App\Services\Core\Sms\Chuanglan\ChuanglanService;
use App\Services\Core\Sms\Dahan\DahanService;
use App\Services\Core\Sms\SmsService;
use Illuminate\Http\Request;
use App\Helpers\Http\HttpClient;

/**
 * 注册发短信控制器
 *
 * Class SmsController
 * @package App\Http\Controllers\V1
 */
class SmsController extends Controller
{

    /**
     * 发送短信
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function SendCode(Request $request)
    {
        // 接收参数
        #接收参数
        $mobile = $request->input('userphone');
        $data = $request ->all();
//
        #检查一分钟是否有重复发送
        $check = SmsFactory::checkCodeExistenceTime($mobile);
//
        if (!$check)
        {
            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(1201), 1201);
        }


//        $ipNums = SmsFactory::limitIp();
//
//        if ($ipNums >= 18)
//        {
//            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(7109), 7109);
//        }
//        $mobileNums =SmsFactory::limitMobile($mobile);
//
//        if ($mobileNums >= 6 )
//        {
//            return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(7109), 7109);
//        }

        #检查用户是否存在
        $user =  UserFactory::getUserByMobile($mobile);
        $userData['userphone'] = $mobile;

        #如果不存在的话，则向用户主表中插入一条数据，
        if ($user==false || empty($user->token)) {

            UserFactory::createUser($userData['userphone'],$data['app_name'],$data['terminal']);
            #发送短信
            // 发送短信
            $re = DahanService::send($mobile);
            // 返回结果
            if (!$re) {
                $data['is_user'] = 1;
                $data['isSucess'] = "0";
                $data['msg'] = "短信验证码发送失败";
                return RestResponseFactory::ok($data, RestUtils::getErrorMessage(1205), 1205);
            }


            $data['is_user'] = 1;
            $data['isSucess'] = "1";
            $data['msg'] = "短信验证码发送成功";
            return RestResponseFactory::ok($data);
        }

        //根据用户的主键生成token
        $token['lass_time'] = DateUtils::getToday();
        //把用户的唯一标识保存到数据库内
        $result = UserFactory::addUserTokenByMobile($data['userphone'],$token);

        if($result)
        {
            $channel_h5 = $request->input('channel');
            if(isset($data['terminal']) && $data['terminal'] == 1){

                $channel['nid'] = isset($channel_h5) ? $request->input('channel') : 'weixin1';
            }else{
                $channel['nid'] = $request->header('channel') ? : 'weixin1';
            }
            $channel['userphone'] = $mobile;

            // 统计注册人次
            $channel['type'] = 3;
            //统计用户渠道
            event(new UserCountChannelEvent($channel));
            $token['token'] = $user->token;
            $token['is_user'] = 0;
            return RestResponseFactory::ok($token,200);

        }

//        return RestResponseFactory::ok(RestUtils::getStdObj(), RestUtils::getErrorMessage(1101), 1101);


    }



    public function getUser(Request $request)
    {

        $userphone = $request -> input('mobile');
        $data['userphone'] = $userphone;
        $data['lass_time'] = DateUtils::getToday();
        User::updateOrCreate(['userphone'=>$userphone],$data);
    }



}
